package com.hose.pages;

import cn.hutool.core.util.StrUtil;
import com.hose.util.HoseUtils;
import com.microsoft.playwright.ElementHandle;
import com.microsoft.playwright.Page;
import io.qameta.allure.Step;

/**
 *
 */
public class MainPage {

    /**
     * 展开菜单
     * @param page
     */
    @Step("展开菜单")
    public MainPage expandMenu(Page page) {
        ElementHandle elementHandle = page.querySelector("//*[@id='immersivebtn_wrapper']/*[1]");
        String attribute = elementHandle.getAttribute("class");
        if (StrUtil.contains(attribute, "display-none")) {
            page.locator("//*[@id='immersivebtn_wrapper']").click();
        }
        return this;
    }

    @Step("进入菜单：{parentMenu} - {menuName}")
    public void enterMenu(Page page, String parentMenu, String menuName) {
        this.expandMenu(page);
        page.waitForTimeout(300);
        ElementHandle targetParentMenu = page.querySelector(StrUtil.format("//*[@role='menuitem' and .//text()='{}']", parentMenu));
        String attributeClass = targetParentMenu.getAttribute("class");
        if (!StrUtil.contains(attributeClass, "submenu-open")) {
            targetParentMenu.click();
        }
        page.waitForTimeout(300);
        ElementHandle targetMenu = page.querySelector(StrUtil.format("//*[starts-with(@class,'ant-menu-item') and .//text()='{}']", menuName));
        String subAttributeClass = targetMenu.getAttribute("class");
        if (!StrUtil.contains(subAttributeClass, "item-selected")) {
            targetMenu.click();
        }
        HoseUtils.wait(page);
    }

    public static MainPage build() {
        return new MainPage();
    }

}
